package com.easyctba.core.service.impl;

import com.easyctba.common.core.domain.R;
import com.easyctba.common.core.utils.MapstructUtils;
import com.easyctba.common.core.utils.StringUtils;
import com.easyctba.common.mybatis.core.page.TableDataInfo;
import com.easyctba.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.easyctba.common.redis.utils.RedisUtils;
import com.easyctba.core.domain.bo.FiasAddPolylineBo;
import com.easyctba.core.domain.bo.FiasClassAddBo;
import com.easyctba.core.enums.FiasRedisEnums;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.easyctba.core.domain.bo.CtbaFiasClassBo;
import com.easyctba.core.domain.vo.CtbaFiasClassVo;
import com.easyctba.core.domain.CtbaFiasClass;
import com.easyctba.core.mapper.CtbaFiasClassMapper;
import com.easyctba.core.service.ICtbaFiasClassService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * fias分类Service业务层处理
 *
 * @author Huang
 * @date 2024-03-22
 */
@RequiredArgsConstructor
@Service
public class CtbaFiasClassServiceImpl implements ICtbaFiasClassService {

    private final CtbaFiasClassMapper baseMapper;

    /**
     * 查询fias分类
     */
    @Override
    public CtbaFiasClassVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询fias分类列表
     */
    @Override
    public TableDataInfo<CtbaFiasClassVo> queryPageList(CtbaFiasClassBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CtbaFiasClass> lqw = buildQueryWrapper(bo);
        Page<CtbaFiasClassVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询fias分类列表
     */
    @Override
    public List<CtbaFiasClassVo> queryList(CtbaFiasClassBo bo) {
        LambdaQueryWrapper<CtbaFiasClass> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CtbaFiasClass> buildQueryWrapper(CtbaFiasClassBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CtbaFiasClass> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCategoryId() != null, CtbaFiasClass::getCategoryId, bo.getCategoryId());
        lqw.like(StringUtils.isNotBlank(bo.getClassName()), CtbaFiasClass::getClassName, bo.getClassName());
        return lqw;
    }

    /**
     * 新增fias分类
     */
    @Override
    public Boolean insertByBo(CtbaFiasClassBo bo) {
        CtbaFiasClass add = MapstructUtils.convert(bo, CtbaFiasClass.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改fias分类
     */
    @Override
    public Boolean updateByBo(CtbaFiasClassBo bo) {
        CtbaFiasClass update = MapstructUtils.convert(bo, CtbaFiasClass.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CtbaFiasClass entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除fias分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据category id获取小类信息
     *
     * @param id category id
     * @return CtbaFiasClasses
     */
    @Override
    public List<CtbaFiasClass> selectByCategoryId(Long id) {
        LambdaQueryWrapper<CtbaFiasClass> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CtbaFiasClass::getCategoryId, id);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public R<Void> addClass(FiasClassAddBo bo) {
        Long id = bo.getId();
        String classNum = bo.getClassNum();
        String name = bo.getName();
        //判断是否重复
        LambdaQueryWrapper<CtbaFiasClass> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CtbaFiasClass::getCategoryId, id)
            .and(i -> i.eq(CtbaFiasClass::getClassNum, classNum)
                .or()
                .eq(CtbaFiasClass::getClassName, name));
        CtbaFiasClass ctbaFiasClass = this.baseMapper.selectOne(wrapper);
        if (ctbaFiasClass!=null){
            return R.fail("FIAS分类名或者分类编号重复");
        }
        CtbaFiasClass ctbaFiasClass1 = new CtbaFiasClass();
        ctbaFiasClass1.setClassNum(classNum);
        ctbaFiasClass1.setClassName(name);
        ctbaFiasClass1.setCategoryId(id);
        return this.baseMapper.insert(ctbaFiasClass1)>0?R.ok():R.fail("添加失败");
    }

    /**
     * 删除编码
     * @param id 编码id
     * @return NULL
     */
    @Override
    public R<Void> delete(Long id) {
        return this.baseMapper.deleteById(id)>0?R.ok():R.fail("删除失败");
    }

    @Override
    public R<String> getNum(Long id) {
        CtbaFiasClass ctbaFiasClass = baseMapper.selectById(id);
        if (ctbaFiasClass!=null){
            return R.ok("操作成功！",ctbaFiasClass.getClassNum());
        }
        return R.fail("编码编号获取失败");
    }

    /**
     * 根据category获取class信息
     * @param ids category ids
     * @return Class数组
     */
    public List<CtbaFiasClass> listClassByCategoryId(List<Long> ids){
        LambdaQueryWrapper<CtbaFiasClass> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CtbaFiasClass::getCategoryId,ids)
            .orderByAsc(CtbaFiasClass::getCreateTime);
        return baseMapper.selectList(wrapper);
    }

    /**
     * 根据ids获取信息
     * @param classIds ids
     * @return CLASS信息
     */
    public List<CtbaFiasClass> selectByIds(List<Long> classIds) {
        return baseMapper.selectBatchIds(classIds);
    }

    /**
     * 根据大类id分别获取其下的编码数量
     * @param longs ids
     * @return 小类编码数量list
     */
    @Override
    public List<Long> getClassSizeByCategoryIds(List<Long> longs) {
        List<Long> result = new ArrayList<>();
        for (Long aLong : longs) {
            LambdaQueryWrapper<CtbaFiasClass> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CtbaFiasClass::getCategoryId,aLong);
            Long size = baseMapper.selectCount(wrapper);
            result.add(size);
        }
        return result;
    }
}
